﻿JsUtil = function () {
    this.toggleShowHide = function (show, item) {
        if (item == undefined)
            return;
        var waitItem = $(item);
        if (show == true && waitItem.css("visibility") != "visible") {
            waitItem.css("visibility", "visible");
        }

        if (show == false && waitItem.css("visibility") != "hidden") {
            waitItem.css("visibility", "hidden");
        }
    }

    this.AddAntiForgeryToken = function (data) {
        data.__RequestVerificationToken = $('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val();
        return data;
    };


    this.selectSelectableElements = function (selectableContainer, elementsToSelect) {
        // add unselecting class to all elements in the styleboard canvas except the ones to select
        $(".ui-selected", selectableContainer).not(elementsToSelect).removeClass("ui-selected").addClass("ui-unselecting");

        // add ui-selecting class to the elements to select
        $(elementsToSelect).not(".ui-selected").addClass("ui-selecting");

        // trigger the mouse stop event (this will select all .ui-selecting elements, and deselect all .ui-unselecting elements)
        selectableContainer.data("selectable")._mouseStop(null);
    }
}

JsUtil = new JsUtil();

Array.prototype.singleOrDefault = function (selFnc) {
    for (var i = 0; i < this.length; i++) {
        if (selFnc(this[i]))
            return this[i];
    }

    return undefined;
}

Array.prototype.single = function (selFuc) {
    var output = this.singleOrDefault(selFuc);
    if (output == undefined)
        throw "Array doesn't contain such item";
    return output;
}

Array.prototype.where = function (selFunc) {
    var output = [];
    for (var i = 0; i < this.length; i++) {
        if (selFunc(this[i]))
            output.push(this[i]);
    }

    return output;
}

Array.prototype.contains = function (item) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == item)
            return true;
    }
    return false;
}

String.prototype.startWith = function (txt) {
    return this.indexOf(txt) == 0;
}

String.prototype.replaceAll = function (token, newToken, ignoreCase) {
    var str, i = -1, _token;
    if ((str = this.toString()) && typeof token === "string") {
        _token = ignoreCase === true ? token.toLowerCase() : undefined;
        while ((i = (
            _token !== undefined ?
                str.toLowerCase().indexOf(
                            _token,
                            i >= 0 ? i + newToken.length : 0
                ) : str.indexOf(
                            token,
                            i >= 0 ? i + newToken.length : 0
                )
        )) !== -1) {
            str = str.substring(0, i)
                    .concat(newToken)
                    .concat(str.substring(i + token.length));
        }
    }
    return str;
};
